from django.db import models
from django.contrib.auth.models import AbstractUser
from django.utils.translation import gettext_lazy as _

class User(AbstractUser):
    """
    自定义用户模型，扩展Django默认用户模型
    """
    phone = models.CharField(_('phone number'), max_length=15, blank=True)
    organization = models.CharField(_('organization'), max_length=100, blank=True)
    api_key = models.CharField(_('API key'), max_length=64, blank=True)
    is_premium = models.BooleanField(_('premium user'), default=False)
    created_at = models.DateTimeField(_('created at'), auto_now_add=True)
    updated_at = models.DateTimeField(_('updated at'), auto_now=True)

    class Meta:
        verbose_name = _('user')
        verbose_name_plural = _('users')

    def __str__(self):
        return self.username

class UserPreference(models.Model):
    """
    用户偏好设置
    """
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='preferences')
    data_sources = models.JSONField(_('data sources'), default=dict)
    visualization_settings = models.JSONField(_('visualization settings'), default=dict)
    notification_settings = models.JSONField(_('notification settings'), default=dict)
    
    created_at = models.DateTimeField(_('created at'), auto_now_add=True)
    updated_at = models.DateTimeField(_('updated at'), auto_now=True)

    class Meta:
        verbose_name = _('user preference')
        verbose_name_plural = _('user preferences')

    def __str__(self):
        return f"{self.user.username}'s preferences"
